The square root of value - the length of a right triangle’s hypotenuse once you already have dx² + dy², or anywhere else a real square root is needed.
sqrt(value) -> real| Name | Type |
|---|---|
value | real |
real
The square root of value. The commonest reason to reach for this by hand is the classic distance formula - the square root of dx² + dy² - but it works for anything a real square root is needed for.
If "the distance between two points" is genuinely the question, reach for dist_to instead of writing the formula out - it answers the same question and safely sidesteps the overflow trap described below.
var dx = x - xstart;
var dy = y - ystart;
var distance = sqrt((dx * dx) + (dy * dy));How far this object has wandered from wherever it spawned - the textbook formula, by hand. Safe here because dx/dy are small; see the warning just below for when this exact formula stops being safe.
real numbers have about 20 bits for their whole-number part - roughly ±524,000. Squaring a value bigger than about 700 before taking its square root overflows that range - not a crash, a wrong answer. dx = 1000 alone already gives dx * dx = 1,000,000, well past the edge. A room only a couple of screens wide can produce a dx that large - this is not a corner case, it is the first thing anyone hits writing the distance formula by hand for anything but a short reach. dist_to exists specifically because it avoids this (it squares in whole pixels first, which has far more headroom, before ever touching real) - prefer it over this formula whenever the two points might be far apart.
sqrt is exact to within 1/64 (0.015625) of the true answer, and it always rounds down, never up or to the nearest - sqrt(2) is 1.40625, not the 1.41 a calculator would show. This is half of real's usual 12 fractional bits, the trade the Game Boy Advance module's own square root makes to stay safe against overflow for any value real can hold at all, rather than only small ones.
Negative input has no real square root; rather than crash, it returns 0.
the GBA module’s own overflow-safe fixed-point sqrt - safe for any value real can hold, but only precise to 1/64 (0.015625), always rounded DOWN, never up (half of real’s usual 12 fractional bits, the trade-off the GBA module makes for that overflow safety). A negative value has no real square root and returns 0 rather than crashing. Squaring a value yourself before calling this can silently overflow real’s own ~524,000-unit range past just a few hundred pixels - reach for dist_to instead whenever "the distance between two points" is the actual question; it avoids exactly that trap.